package com.netnovelstore.config;

import com.netnovelstore.interceptor.AdminInterceptor;
import com.netnovelstore.interceptor.JwtInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration

public class InterceptorConfigurerAdapter implements WebMvcConfigurer {

    private AdminInterceptor adminInterceptor;
    private JwtInterceptor jwtInterceptor;

    @Autowired
    public InterceptorConfigurerAdapter(AdminInterceptor adminInterceptor, JwtInterceptor jwtInterceptor) {
        this.adminInterceptor = adminInterceptor;
        this.jwtInterceptor = jwtInterceptor;
    }


    @Value("${web.resource-path}")
    private String resourcePath;

     @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决静态资源无法访问
        registry.addResourceHandler("/**").addResourceLocations("file:"+this.resourcePath);
        // 解决swagger无法访问
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(adminInterceptor)
                .addPathPatterns("/Manage/**");
        // 对所有访问路径，都通过jwtInterceptor类型的拦截器进行拦截
        registry.addInterceptor(jwtInterceptor).addPathPatterns("/User/**").addPathPatterns("/Book/**").addPathPatterns("/Event/**")
                .excludePathPatterns("/static/**", "/alipay/**", "/User/login", "/swagger-ui.html", "/webjars/**", "/User/register", "/User/isExistByUserName", "/kaptcha/**", "/User/addProfile", "/js/**", "/fonts/**");
        //放行登录页，登陆操作，静态资源
    }
}
